package me.hunche.app.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/*
 * Created on 2005-4-7 
 * 
 */


public class FreeMarkerUtility {

	private final static Log log = LogFactory.getLog(FreeMarkerUtility.class);
	private static Configuration freemarkerCfg = null;
	private static String templatePath = "/templates";

	public static boolean creatDirs(String aParentDir, String aSubDir) {
		File aFile = new File(aParentDir);
		if (aFile.exists()) {
			File aSubFile = new File(aParentDir + aSubDir);
			if (!aSubFile.exists()) {
				return aSubFile.mkdirs();
			} else {
				return true;
			}
		} else {
			return false;
		}
	}

	public static boolean creatDirs(String dir) {
		File aFile = new File(dir);
		if (aFile.exists()) {
			return true;
		} else {
			aFile.mkdirs();
			return true;
		}
	}

	protected static Configuration getFreeMarkerCFG() {
		if (null == freemarkerCfg) {
			// Initialize the FreeMarker configuration;
			// - Create a configuration instance
			freemarkerCfg = new Configuration();
			freemarkerCfg.setClassForTemplateLoading(FreeMarkerUtility.class, templatePath);
		}

		return freemarkerCfg;
	}

	public static boolean geneHtmlFile(String templateFileName, Map propMap, String htmlFilePath, String htmlFileName) {

		try {
			Template t = getFreeMarkerCFG().getTemplate(templateFileName);
			creatDirs(htmlFilePath);
			File afile = new File(htmlFilePath + "/" + htmlFileName);
			Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile)));
			t.process(propMap, out);
			log.info("Successfully generate html !");
		} catch (TemplateException e) {
			log.error("Error while processing FreeMarker template " + templateFileName, e);
			return false;
		} catch (IOException e) {
			log.error("Error while generate Static Html File " + htmlFileName, e);
			return false;
		}
		return true;
	}

	public static String geneHtmlString(String templateFileName, Map propMap) {

		try {
			Template template = getFreeMarkerCFG().getTemplate(templateFileName);
			StringWriter out = new StringWriter();
			template.process(propMap, out);
			return out.getBuffer().toString();
		} catch (TemplateException e) {
			throw new RuntimeException(":::[geneHtmlString]Template error, msg->" + e.getMessage(), e);
		} catch (IOException e) {
			throw new RuntimeException(":::[geneHtmlString]unknown error, msg->" + e.getMessage(), e);
		}
	}

	public static void main(String[] args) {
		// Email aItem = new Email();
		// aItem.setReceiver("ABC");
		// String[] attachmentPaths = {"http://111.html"};
		// aItem.setAttachmentUrls(attachmentPaths) ;
		//
		// FreeMarkerUtility test = new FreeMarkerUtility();
		//
		// Map root = new HashMap();
		// root.put("email", aItem);
		// String sFileName = "1.htmL";
		//
		// boolean bOK = test.geneHtmlFile("email.ftl", root, generatePath,
		// sFileName);
		// String emailStr = test.geneHtmlString("email.ftl", root);
		// System.out.println(emailStr);
	}
}
